<template>
  <view class="vm-page padding-y bg-efefef vh">
    <view class="btn-container">
      <nut-button type="primary" block @click="getWeixnCode"
        >获取登录Code</nut-button
      >
    </view>
    <view class="btn-container">
      <nut-button type="primary" block @click="getUserProfile"
        >获取用户信息</nut-button
      >
    </view>
    <view class="btn-container">
      <!-- 默认按钮，使用 nut 样式 -->
      <button
        class="nut-button nut-button--primary nut-button--normal nut-button--round nut-button--block h5-view"
        open-type="getPhoneNumber"
        @getphonenumber="getUserPhoneNumber"
      >
        获取用户手机号
      </button>
    </view>
    <view class="btn-container">
      <nut-button type="primary" block @click="apiTest">接口测试</nut-button>
    </view>
    <view class="btn-container">
      <nut-button type="primary" block @click="apiTest2"
        >用户绑定接口</nut-button
      >
    </view>
    <view class="btn-container">
      <nut-button type="primary" block @click="apiTest3"
        >获取课程列表</nut-button
      >
    </view>
    <view class="btn-container">
      <nut-button type="primary" block @click="apiTest4"
        >获取用户所有班级列表</nut-button
      >
    </view>
  </view>
</template>

<script lang="ts">
import Taro from "@tarojs/taro";

const appId = "wx41fec992e8e1d11b";
const basUrl = "http://127.0.0.1:9801";
export default {
  name: "Index",
  components: {},
  data() {
    return {
      unionId: "",
      studentNo: "21100000011",
      openId: "",
      token: "",
      isAuth: "",
      wxCode: "",
      studentName: "微信测试用户",
      idCard: "350521199111058022",
      lessonId: "1385402229812486146",
    };
  },
  methods: {
    getWeixnCode() {
      Taro.login().then((res) => {
        if (res.code) {
          this.wxCode = res.code;
          console.log("user-code: %s", res.code);
        }
      });
    },
    getUserProfile() {
      let self = this;
      Taro.getUserProfile({
        desc: "用于完善会员资料", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (v) => {
          console.log(
            "接口数据进来了code[]" +
              this.wxCode +
              "appId[]" +
              appId +
              "basUrl[]" +
              basUrl +
              " unionId[]" +
              this.unionId
          );
          console.log(v);
          /**
           * 一键登录带授权
           * */
          Taro.request({
            url: basUrl + "/api/wxApp/autoLogin", //仅为示例，并非真实的接口地址
            method: "GET",
            data: {
              code: this.wxCode,
              appId: appId,
              encryptedData: v.encryptedData,
              iv: v.iv,
              rawData: v.rawData,
              signature: v.signature,
            },
            header: {
              "content-type": "application/json", // 默认值
            },
            success: function(res) {
              console.log(res.data);
              console.log("statusCode[]" + res.statusCode);
              if (res.statusCode == 200) {
                console.log("data[]" + res.data);
                let reqData = res.data.data;
                self.unionId = reqData.unionId;
                self.openId = reqData.openId;
                if (reqData.isAuth == 1) {
                  self.isAuth = reqData.isAuth; //用户是否绑定 (绑定成功时 不需要再进行绑定，未绑定时跳转绑定页面)
                  self.studentNo = reqData.studentNo; //绑定后才会有学生编号
                  self.token = reqData.token; //未绑定的用户不存在token
                }
              } else {
                console.log("errMsg: %s", res.errMsg);
              }
            },
          });
          // this.getLoginHttp(res);
          console.log("user-profile: %s", JSON.stringify(v.userInfo));
        },
      });
    },
    getLoginHttp() {},
    getUserPhoneNumber(e) {
      let phoneInfoEncryptedData = e.detail;
      console.log("user-phone: %s", JSON.stringify(phoneInfoEncryptedData));
    },
    apiTest() {
      // 使用 taro request 发请求
      // Taro.request({
      //   url: "test.php", //仅为示例，并非真实的接口地址
      //   data: {
      //     x: "",
      //     y: "",
      //   },
      //   header: {
      //     "content-type": "application/json", // 默认值
      //   },
      //   success: function(res) {
      //     console.log(res.data);
      //   },
      // });

      // 使用 API 发请求
      this.$api.test.game().then((data) => {
        console.log("api-testgame: %s", JSON.stringify(data));
      });
    },
    apiTest2() {
      console.log("openId[]" + this.openId);
      let self = this;

      /**
       * 用户绑定接口
       */
      Taro.request({
        url: basUrl + "/api/wxApp/userBingding", //仅为示例，并非真实的接口地址
        method: "GET",
        data: {
          openId: self.openId, //open ID
          studentName: self.studentName, //学生名字
          studentNo: self.studentNo, //学生编号
          idCard: self.idCard, //学生身份证
        },
        header: {
          "content-type": "application/json", // 默认值
        },
        success: function(res) {
          console.log(res.data);
          console.log("statusCode[]" + res.statusCode);
          if (res.statusCode == 200) {
            console.log("data[]" + res.data);
            // this.studentNo=res.data.studentNo;//绑定后才会有学生编号
            if (res.data.isAuth == 1) {
              console.log("isAuth: %s", res.data.isAuth); //是否授权 0未绑定 1 已绑定
              console.log("studentNo: %s", res.data.studentNo);
              console.log("token: %s", res.data.token);
              self.token = res.data.token; //登录后产生的token 验证使用
              self.studentNo = res.data.studentNo; //学生编号
              self.isAuth = res.data.isAuth; //是否绑定
            } else {
              console.log("errMsg: %s", res.errMsg);
            }
          } else {
            console.log("errMsg: %s", res.errMsg);
          }
        },
      });
      console.log("执行测试接口2");
    },
    apiTest3() {
      let self = this;
      /**
       * 获取所有课程列表
       */
      Taro.request({
        url: basUrl + "/api/lelesson/list", //仅为示例，并非真实的接口地址
        method: "GET",
        data: {
          token: self.token,
        },
        header: {
          "X-Access-Token": self.token,
          "content-type": "application/json", // 默认值
        },
        success: function(res) {
          console.log(res.data);
          console.log("statusCode[]" + res.statusCode);
          if (res.statusCode == 200) {
            console.log("data[]" + res.data);
          } else {
            console.log("errMsg: %s", res.errMsg);
          }
        },
      });
    },
    apiTest4() {
      let self = this;
      /**
       * 获取所有课程列表
       */
      Taro.request({
        url: basUrl + "/api/wxStudent/studentClass", //仅为示例，并非真实的接口地址
        method: "GET",
        data: {
          studentNo: self.studentNo,
          lessonId: self.lessonId,
        },
        header: {
          "X-Access-Token": self.token,
          "content-type": "application/json", // 默认值
        },
        success: function(res) {
          console.log(res.data);
          console.log("statusCode[]" + res.statusCode);
          if (res.statusCode == 200) {
            console.log("data[]" + res.data);
          } else {
            console.log("errMsg: %s", res.errMsg);
          }
        },
      });
    },
  },
  mounted() {},
  onReady() {},
  onShow() {},
  onHide() {},
};
</script>

<style lang="scss">
.btn-container {
  padding: 10px 20px;
}
</style>
